package controller

import (
	"encoding/json"
	"github.com/go-openapi/runtime"
	"github.com/go-openapi/runtime/middleware"
	"mlss-cc-a/models"
	"mlss-cc-a/restapi/operations/person"
	service "mlss-cc-a/service/person"
	"net/http"
)

func GetPersonList(params person.GetPersonListParams) middleware.Responder {
	list, err := service.GetPersonList(params)
	if err != nil {
		return ResponderFunc(err)
	}

	//marshal, _ := json.Marshal(list)
	//logger.Logger().Infoln(marshal)

	return ReturnPersonList(http.StatusOK, "", list)
}

func ReturnPersonList(code int, message string, list *models.PersonList) middleware.Responder {
	if message == "" {
		message = "success"
	}

	return middleware.ResponderFunc(func(writer http.ResponseWriter, producer runtime.Producer) {
		writer.WriteHeader(code)
		marshal, _ := json.Marshal(list)
		writer.Write(marshal)
	})
}
